package cn.itsource.crm.web.controller;

import cn.itsource.crm.domain.Employee;
import cn.itsource.crm.domain.Permission;
import cn.itsource.crm.query.PermissionQuery;
import cn.itsource.crm.service.IPermissionService;
import cn.itsource.shiro.utils.LoginUserUtil;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.PageList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/permission")
@CrossOrigin//解决跨域问题
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有权限信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public List<Permission> list(){
        List<Permission> all = permissionService.findAll();
        return all;
    }

    /**
     * 通过角色id查询所有权限
     * @return
     */
    @RequestMapping(value = "/findAllByRoleId",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有权限信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public List<Permission> findAllByRoleId(){
        /*获取租户角色id*/
        Employee user = LoginUserUtil.getUser();
        Long id = user.getRole().getId();
        List<Permission> all = permissionService.findAllByRoleId(id);
        return all;
    }

    /**
     * 分页和高级查询
     * @return
     */
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "list查询所有方法",notes = "查询所有权限信息")
    //HTTP响应整体描述
    @ApiResponses({
            //HTTP响应其中1个描述
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 500,message = "网络错误，请稍后")
    })
    public PageList<Permission> queryPage(@RequestBody PermissionQuery permissionQuery){
        PageList<Permission> list = permissionService.queryPage(permissionQuery);
        return list;
    }

    /**
     * 根据id查询单个权限信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findOne/{id}",method = RequestMethod.GET)
    @ResponseBody
    //描述一个类的一个方法，或者说一个接口
    @ApiOperation(value = "根据id查询权限信息",notes = "getOne查询方法")
    //一个请求参数
    @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    public Permission findOne(@PathVariable Long id){
        return permissionService.findOne(id);
    }

    /**
     * 保存权限信息
     * @param permission
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.PUT)//保存用PUT
    @ResponseBody
    @ApiOperation(value = "save保存权限信息的方法",notes = "保存权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "permission",value = "permission对象",dataType = "Permission",paramType = "path")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "保存成功"),
            @ApiResponse(code = 500,message = "服务器错误")
    })
    public AjaxResult save(@RequestBody Permission permission){
        try {
            permissionService.save(permission);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult(false);
            result.setMsg("操作失败"+e.getMessage());
            return result;
        }
    }

    /**
     * 更新权限信息
     * @param permission
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)//POST用来修改信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "更新成功"),
            @ApiResponse(code = 500,message = "网络错误...")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "permission权限信息",value = "权限信息",dataType = "Permission",paramType = "path")
    })
    public AjaxResult update(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }


    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)//DELETE删除权限信息
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200,message = "删除成功"),
            @ApiResponse(code = 500,message = "服务器异常")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "参数id",value = "参数id",dataType = "Long",paramType = "path",required = true)
    })
    @ApiOperation(value = "delete删除方法",notes = "删除权限信息")
    public AjaxResult delete(@PathVariable Long id){
        try {
            permissionService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false);
        }
    }
}
